home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_std / std_file_write.e < prev    next >
Text File  |  1997-04-13  |  4KB  |  212 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class STD_FILE_WRITE
  5. --
  6. -- Basic output facilities to write a named file on the disk.
  7. --
  8. -- Note : most features are common with STD_OUTPUT so you can 
  9. --        test your program first on the screen and then, changing 
  10. --        of instance (STD_OUTPUT/STD_FILE_WRITE), doing the same
  11. --        on a file.
  12. --
  13.    
  14. inherit STD_FILE
  15.    
  16. creation 
  17.    connect_to, make
  18.    
  19. feature 
  20.  
  21.    connect_to(new_path: STRING) is
  22.       do
  23.          make;
  24.      output_stream := fopen(new_path,mode);
  25.      if output_stream.is_not_void then
  26.         path := new_path;
  27.      end;
  28.       end;
  29.    
  30.    disconnect is
  31.       local
  32.      err: INTEGER;
  33.       do
  34.      err := fclose(output_stream); 
  35.      path := Void;
  36.       end;
  37.    
  38.    make is
  39.       do
  40.            mode := "w";
  41.       end;
  42.  
  43. feature    
  44.    
  45.    put_character(c: CHARACTER) is
  46.       local
  47.      err: CHARACTER;
  48.       do
  49.      err := fputc(c,output_stream);
  50.      debug
  51.         if err /= c then
  52.            std_error.put_string("Error in STD_FILE_WRITE.put_character.%N");
  53.            crash;
  54.         end;
  55.      end;
  56.       end;
  57.  
  58.    put_string(s: STRING) is
  59.      -- Output `s' to current output device.
  60.       require
  61.      s /= Void;
  62.       local
  63.      i: INTEGER;
  64.       do
  65.      from  
  66.         i := 1;
  67.      until
  68.         i > s.count
  69.      loop
  70.         put_character(s.item(i));
  71.         i := i + 1;
  72.      end;
  73.       end;
  74.    
  75.    put_integer (i: INTEGER) is
  76.      -- Output `i' to current output device.
  77.       do
  78.      tmp_string.clear;
  79.      i.append_in(tmp_string);
  80.      put_string(tmp_string);
  81.       end;
  82.    
  83.    put_integer_format(i, s: INTEGER) is
  84.      -- Output `i' to current output device using at most
  85.      -- `s' character.
  86.       do
  87.      tmp_string.clear;
  88.      i.append_in_format(tmp_string,s);
  89.      put_string(tmp_string);
  90.       end;
  91.    
  92.    put_real(r: REAL) is
  93.      -- Output `r' to current output device.
  94.       do
  95.      tmp_string.clear;
  96.      r.append_in(tmp_string);
  97.      put_string(tmp_string);
  98.       end;
  99.    
  100.    put_real_format(r: REAL; f: INTEGER) is
  101.      -- Output `r' with only `f' digit for the fractionnal part.
  102.      -- Examples: 
  103.      --    put_real(3.519,2) print "3.51". 
  104.       require
  105.      f >= 0;
  106.       do
  107.      tmp_string.clear;
  108.      r.append_in_format(tmp_string,f);
  109.      put_string(tmp_string);
  110.       end;
  111.    
  112.    put_double(d: DOUBLE) is
  113.      -- Output `d' to current output device.
  114.       do
  115.      tmp_string.clear;
  116.      d.append_in(tmp_string);
  117.      put_string(tmp_string);
  118.       end;
  119.    
  120.    put_double_format(d: DOUBLE; f: INTEGER) is
  121.      -- Output `d' with only `f' digit for the fractionnal part.
  122.      -- Examples: 
  123.      --    put_double(3.519,2) print "3.51". 
  124.       require
  125.      f >= 0;
  126.       do
  127.      tmp_string.clear;
  128.      d.append_in_format(tmp_string,f);
  129.      put_string(tmp_string);
  130.       end;
  131.    
  132.    put_boolean(b: BOOLEAN) is
  133.      -- Output `b' to current output device according
  134.      -- to the Eiffel format.
  135.       do
  136.      if b then
  137.         put_string("true");
  138.      else
  139.         put_string("false");
  140.      end;
  141.       end;
  142.    
  143.    put_new_line is
  144.      -- Output a newline character.
  145.       do
  146.      put_character('%N');
  147.       end;
  148.    
  149.    put_spaces(nb: INTEGER) is
  150.       -- Output `nb' spaces character.
  151.       require
  152.      nb >= 0;
  153.       local
  154.      count : INTEGER;
  155.       do
  156.      from  
  157.      until
  158.         count >= nb
  159.      loop
  160.         put_character(' ');
  161.         count := count + 1;
  162.      end;
  163.       end; 
  164.    
  165.    append_file(file_name: STRING) is
  166.       require
  167.      file_exists(file_name);
  168.       local
  169.      c: CHARACTER;
  170.       do
  171.      tmp_file_read.connect_to(file_name);
  172.      from  
  173.         tmp_file_read.read_character;
  174.      until
  175.         tmp_file_read.end_of_input
  176.      loop
  177.         c := tmp_file_read.last_character;
  178.         put_character(c);
  179.         tmp_file_read.read_character;
  180.      end;
  181.      tmp_file_read.disconnect;
  182.       end;
  183.  
  184.    flush is
  185.       local
  186.      err: INTEGER;
  187.       do
  188.      err := fflush(output_stream);
  189.       end;
  190.  
  191. feature {NONE}
  192.    
  193.    tmp_file_read: STD_FILE_READ is
  194.       once
  195.      !!Result.make;
  196.       end;
  197.    
  198. feature {NONE}
  199.    --
  200.    -- NOTE: use only a few basic ANSI C functions.
  201.    -- Try to use as few external C calls as possible.
  202.    --
  203.    
  204.    output_stream: POINTER;
  205.    
  206.    tmp_string: STRING is
  207.       once
  208.      !!Result.make(512);
  209.       end;
  210.    
  211. end -- STD_FILE_WRITE
  212.